<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>晶莹果甜文本语音转换服务</title>
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
    <style>
        .loading {
            display: none;
            animation: spin 1s linear infinite;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        .loading.active {
            display: inline-block;
        }
    </style>
</head>
<body class="bg-gray-100 min-h-screen">
    <div class="container mx-auto px-4 py-8">
        <h1 class="text-3xl font-bold text-center mb-8 text-gray-800">晶莹果甜文本语音转换服务</h1>
        
        <div class="bg-white rounded-lg shadow-lg p-6 max-w-2xl mx-auto">
            <div class="mb-6">
                <label class="block text-gray-700 text-sm font-bold mb-2" for="voice-select">
                    选择声音
                </label>
                <select id="voice-select" class="w-full px-3 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    <option value="zh-CN-XiaoxiaoNeural">加载中...</option>
                </select>
            </div>
            
            <div class="mb-6">
                <label class="block text-gray-700 text-sm font-bold mb-2" for="text-input">
                    输入文本
                </label>
                <textarea
                    id="text-input"
                    rows="4"
                    class="w-full px-3 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
                    placeholder="请输入要转换的文本..."
                ></textarea>
            </div>
            
            <div class="flex items-center justify-between mb-4">
                <button
                    id="synthesize-btn"
                    class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:ring-2 focus:ring-blue-500"
                >
                    生成语音
                    <svg class="loading w-5 h-5 ml-2" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                        <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                        <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                    </svg>
                </button>
                
                <audio id="audio-player" controls class="ml-4" style="display: none;">
                    您的浏览器不支持音频播放
                </audio>
            </div>
            
            <div id="process-time-container" class="text-sm text-gray-600 mt-2" style="display: none;">
                处理时间: <span id="process-time" class="font-medium">0</span> 毫秒
            </div>

                <!--audio id="audio-player" controls class="ml-4" style="display: block;">
                    <source src="http://127.0.0.1:8000/synthesize?text=%E8%BF%99%E9%87%8C%E6%98%AF%E9%AB%98%E5%8B%87%E5%9C%A8%E8%AF%B4%E8%AF%9D%EF%BC%8C%E8%AF%B7%E6%B3%A8%E6%84%8F%E5%90%AC%E8%A7%A3%E8%AF%B4%E3%80%82This%20is%20gauss%20speaking.&voice=zh-CN-XiaoxiaoNeural" type="audio/mpeg">
                </audio-->
            </div>
        </div>
    </div>

    <script>
        // 获取页面元素
        const voiceSelect = document.getElementById('voice-select');
        const textInput = document.getElementById('text-input');
        const synthesizeBtn = document.getElementById('synthesize-btn');
        const audioPlayer = document.getElementById('audio-player');
        const loadingIcon = document.querySelector('.loading');

        // 加载声音列表
        async function loadVoices() {
            try {
                const response = await fetch('/voices');
                const voices = await response.json();
                
                // 清空选择框
                voiceSelect.innerHTML = '';
                
                // 添加声音选项
                voices.forEach(voice => {
                    const option = document.createElement('option');
                    option.value = voice.ShortName;
                    option.textContent = `${voice.ShortName} (${voice.Gender})`;
                    voiceSelect.appendChild(option);
                });
            } catch (error) {
                console.error('加载声音列表失败:', error);
                alert('加载声音列表失败，请刷新页面重试');
            }
        }

        // 合成语音
        async function synthesizeSpeech() {
            const text = textInput.value.trim();
            if (!text) {
                alert('请输入要转换的文本');
                return;
            }

            // 显示加载动画
            loadingIcon.classList.add('active');
            synthesizeBtn.disabled = true;
            audioPlayer.style.display = 'none';

            try {
                const response = await fetch('/api/synthesize', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        text: text,
                        voice: voiceSelect.value
                    })
                });

                if (!response.ok) {
                    throw new Error('语音合成失败');
                }

                // 获取音频数据
                const blob = await response.blob();
                const audioUrl = URL.createObjectURL(blob);
                
                // 获取处理时间并显示
                const processTime = response.headers.get('X-Process-Time');
                const processTimeContainer = document.getElementById('process-time-container');
                const processTimeSpan = document.getElementById('process-time');
                
                if (processTime) {
                    processTimeSpan.textContent = processTime;
                    processTimeContainer.style.display = 'block';
                }

                // 更新音频播放器
                audioPlayer.src = audioUrl;
                audioPlayer.style.display = 'block';
                audioPlayer.play();

            } catch (error) {
                console.error('语音合成失败:', error);
                alert('语音合成失败，请重试');
            } finally {
                // 隐藏加载动画
                loadingIcon.classList.remove('active');
                synthesizeBtn.disabled = false;
            }
        }

        // 绑定事件
        synthesizeBtn.addEventListener('click', synthesizeSpeech);
        
        // 页面加载完成后获取声音列表
        document.addEventListener('DOMContentLoaded', loadVoices);
    </script>
</body>
</html> 